/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplikasi.com.implement;

import aplikasi.com.entity.Karyawan;
import aplikasi.com.interfac.interKaryawan;
import aplikasi.com.koneksiDatabase.DatabaseUtilities;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Henfryandie
 */
public class implemKaryawan implements interKaryawan {

    public Karyawan insertKar(Karyawan o) throws SQLException {
        String sqlInsert = "INSERT INTO aplikasirentakkomik.karyawan(namaKaryawan,alamat,userKaryawan,passKaryawan,notelpon,akses) VALUES (?,?,?,?,?,?);";
        PreparedStatement prepare = DatabaseUtilities.getConnection().prepareStatement(sqlInsert);
        prepare.setString(1, o.getNamaKaryawan());
        prepare.setString(2, o.getAlamatKaryawan());
        prepare.setString(3, o.getUserKaryawan());
        prepare.setString(4, o.getPassKaryawan());
        prepare.setString(5, o.getNoTelp());
        prepare.setString(6, o.getAkses());
        prepare.executeUpdate();
        return o;
    }

    public void update(Karyawan o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(String nama) throws SQLException {
        String sql = "DELETE FROM aplikasirentakkomik.karyawan WHERE namaKaryawan=?;";
        PreparedStatement prepared = DatabaseUtilities.getConnection().prepareStatement(sql);
        prepared.setString(1, nama);
        prepared.executeUpdate();
    }

    public List<Karyawan> getAll() throws SQLException {
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select * from karyawan");
        List<Karyawan> list = new ArrayList<Karyawan>();
        while (rs.next()) {
            Karyawan o = new Karyawan();
            o.setIdKaryawan(rs.getInt("idKaryawan"));
            o.setNamaKaryawan(rs.getString("namaKaryawan"));
            o.setAlamatKaryawan(rs.getString("alamat"));
            o.setUserKaryawan(rs.getString("userKaryawan"));
            o.setPassKaryawan(rs.getString("passKaryawan"));
            o.setNoTelp(rs.getString("notelpon"));
            o.setAkses(rs.getString("akses"));
            list.add(o);
        }
        return list;
    }

    public Karyawan getByKaryawan(String o) throws SQLException {
        PreparedStatement ps = DatabaseUtilities.getConnection().prepareStatement("select * from karyawan where userKaryawan=?");
        ps.setString(1, o);
        ResultSet rs = ps.executeQuery();
        Karyawan karyawan = new Karyawan();
        if (rs.next()) {
            karyawan.setNamaKaryawan(rs.getString("namaKaryawan"));
            karyawan.setUserKaryawan(rs.getString("userKaryawan"));
            karyawan.setPassKaryawan(rs.getString("passKaryawan"));
            karyawan.setAlamatKaryawan(rs.getString("alamat"));
            karyawan.setNoTelp(rs.getString("notelpon"));
            karyawan.setAkses(rs.getString("akses"));
        }
        return karyawan;
    }
}
